package com.ghck.server.controller.events;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.ghck.server.service.BodypartDictService;
import com.ghck.server.service.DepartmentDictService;
import com.ghck.server.service.DictDiseaseKeyService;
import com.ghck.server.service.DiseaseService;
import com.ghck.server.service.HospitalDictService;
import com.ghck.server.service.RegionDictService;
import com.ghck.server.utils.IntegerUtil;

@Component
public class RedisSyncListener implements ApplicationListener<RedisSyncEvent>{

	
	@Autowired
	RegionDictService regionDictService;

	@Autowired
	HospitalDictService hospitalDictService;
	
	@Autowired
	DepartmentDictService departmentDictService;
	
	@Autowired
	DiseaseService diseaseService;
	
	@Autowired
	BodypartDictService bodypartDictService;
	
	@Autowired
	DictDiseaseKeyService dictDiseaseKeyService;
	
	@Override
	public void onApplicationEvent(RedisSyncEvent event) {
		Object source = event.getSource();
		int type = event.getType();
		Integer sourceId = null;
		if(source instanceof Integer){
			sourceId = (Integer) source;
		}
		
		if(IntegerUtil.isPositiveInteger(sourceId)){
			switch (type) {
			case RedisSyncEvent.DEPARTMENT:
				regionDictService.syncSingletonRedis(sourceId);
				break;
			case RedisSyncEvent.HOSPITAL:
				hospitalDictService.syncSingletonRedis(sourceId);
				break;
			case RedisSyncEvent.REGION:
				regionDictService.syncSingletonRedis(sourceId);
				break;
			case RedisSyncEvent.DISEASE:
				diseaseService.syncSingletonRedis(sourceId);
				break;
			case RedisSyncEvent.BODYPART:
				bodypartDictService.syncInRedis(true);
				break;	
			case RedisSyncEvent.DISEASE_KEY:
				dictDiseaseKeyService.syncInRedis(true);
				break;
			}
		}else{
			switch (type) {
			case RedisSyncEvent.DEPARTMENT:
				regionDictService.syncInRedis(true);
				break;
			case RedisSyncEvent.HOSPITAL:
				hospitalDictService.syncInRedis(true);
				break;
			case RedisSyncEvent.REGION:
				regionDictService.syncInRedis(true);
				break;
			case RedisSyncEvent.DISEASE:
				diseaseService.syncInRedis(true);
				break;
			case RedisSyncEvent.BODYPART:
				bodypartDictService.syncInRedis(true);
				break;
			case RedisSyncEvent.DISEASE_KEY:
				dictDiseaseKeyService.syncInRedis(true);
				break;
			}
		}
		
	}

}
